package com.finchina.myview.viewgroups;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * 作者：黄赛赛 on 2015/9/10 10:42
 *http://blog.csdn.net/lmj623565791/article/details/38339817
 * @version V1.0
 * @Description: ${todo}()
 */
public class CustomImgContainer extends ViewGroup {
//    1、决定该ViewGroup的LayoutParams
//    对于我们这个例子，我们只需要ViewGroup能够支持margin即可，那么我们直接使用系统的MarginLayoutParams
    @Override
    public  ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    public CustomImgContainer(Context context) {
        super(context);
    }

    public CustomImgContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomImgContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        // 计算出所有的childView的宽和高
        measureChildren(widthMeasureSpec,heightMeasureSpec);
        /**
         * 记录如果是wrap_content是设置的宽和高
         */
        int width=0, height = 0;
        int cCount = getChildCount();

        int cWidth = 0, cHeight = 0;
        MarginLayoutParams cParams = null;
        // 用于计算左边两个childView的高度
        int lHeight = 0;
        // 用于计算右边两个childView的高度，最终高度取二者之间大值
        int rHeight = 0;

        // 用于计算上边两个childView的宽度
        int tWidth = 0;
        // 用于计算下面两个childiew的宽度，最终宽度取二者之间大值
        int bWidth = 0;

        /**
         * 根据childView计算的出的宽和高，以及设置的margin计算容器的宽和高，主要用于容器是warp_content时
         */
        for (int i =0 ; i < cCount; i++ ){
            View child = getChildAt(i);
            cWidth = child.getMeasuredWidth();
            cHeight = child.getMeasuredHeight();
            cParams = (MarginLayoutParams) child.getLayoutParams();
            // 上面两个childView
            if (i ==0 || i == 1 ){
                tWidth += cWidth + cParams.leftMargin + cParams.rightMargin;
            }

            if (i ==2 || i == 3){
                bWidth += cWidth + cParams.leftMargin + cParams.rightMargin;
            }

            if ( i == 0 || i == 2){
                lHeight += cHeight + cParams.topMargin + cParams.bottomMargin;
            }

            if ( i == 1 || i == 3){
                rHeight += cHeight + cParams.topMargin + cParams.bottomMargin;
            }
        }

        width = Math.max(tWidth, bWidth);
        height = Math.max(lHeight, rHeight);

        if ( widthMode == MeasureSpec.EXACTLY){
            width = widthSize;
        }
        if (heightMode == MeasureSpec.EXACTLY){
            height = heightSize;
        }


        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int cCount = getChildCount();
        int cWidth = 0;
        int cHeight = 0;
        MarginLayoutParams cParams  = null;
        /**
         * 遍历所有childView根据其宽和高，以及margin进行布局
         */
        for (int i= 0; i <cCount; i++){
            View child = getChildAt(i);
            cWidth = child.getMeasuredWidth();
            cHeight = child.getMeasuredHeight();
            cParams = (MarginLayoutParams) child.getLayoutParams();

            int cl = 0, ct = 0, cr = 0, cb = 0;

            switch (i){
                case 0:
                    cl = cParams.leftMargin;
                    ct = cParams.topMargin;
                    break;
                case 1:
                    cl = getWidth() - cParams.leftMargin - cWidth - cParams.rightMargin;
                    ct = cParams.topMargin;
                    break;
                case 2:
                    cl = cParams.leftMargin;
                    ct = getHeight()- cParams.bottomMargin - cHeight;
                    break;
                case 3:
                    cl = getWidth() -cParams.leftMargin - cWidth - cParams.rightMargin;
                    ct = getHeight() - cParams.bottomMargin - cHeight;
                    break;
            }
            cr = cl + cWidth;
            cb = ct + cHeight;
            child.layout(cl, ct, cr, cb);
        }
    }



}
